package org.despair2D.ui 
{
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import org.despair2D.debug.Logger;
	import org.despair2D.ui.core.IComponent;
	import org.despair2D.ui.events.ManipulateEvent;
	import org.despair2D.ui.Persona;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
	
public class ToolTipManager
{	
	
	/**
	 * 加入提示
	 * @param	C
	 * @param	ToolTipStateRef
	 * 
	 * @usage	可覆盖 !!
	 */
	final public function add( C:IComponent, toolTipStateRef:Class = null ) : void
	{
		if (!m_toolTipList[C])
		{
			C.addEventListener(ManipulateEvent.OVER,   ____onShowToolTip);
			C.addEventListener(ManipulateEvent.KILLED, ____onTargetKilled);
		}
		
		m_toolTipList[C] = toolTipStateRef ? toolTipStateRef : DefaultToolTipState
	}
	
	
	/**
	 * 隐藏提示
	 */
	final public function hide() : void
	{
		if(m_pa)
		{
			m_pa.kill()
			m_pa = null
		}
	}
	
	
	/**
	 * 削除提示
	 * @param	C
	 */
	final public function remove( C:IComponent ) : void
	{
		if (!m_toolTipList[C])
		{
			Logger.reportWarning(this, 'removeTips', '组件未创建提示 !!')
			return
		}
		
		delete m_toolTipList[C];
		
		C.removeEventListener(ManipulateEvent.OVER,   ____onShowToolTip);
		C.removeEventListener(ManipulateEvent.KILLED, ____onTargetKilled);
	}
	
	
	
	final ns_despair function ____onShowToolTip( e:ManipulateEvent ) : void
	{
		var C:IComponent
		var toolTipStateRef:Class
		var state:ToolTipState
		
		C                =  e.target as IComponent;
		toolTipStateRef  =  m_toolTipList[C]
		state            =  new toolTipStateRef()
		state.m_comp     =  C
		
		C.addEventListener(ManipulateEvent.LEAVE, ____onMouseLeave);
		
		if (m_pa)
		{
			m_pa.setState(toolTipStateRef)
		}
		
		else
		{
			m_pa = Persona.createPersona(toolTipStateRef, -1, true)
			m_pa.interactive = false
		}
	}
	
	
	ns_despair function ____onMouseLeave( e:ManipulateEvent ) : void
	{
		var C:IComponent = e.target as IComponent;
		
		C.removeEventListener(ManipulateEvent.LEAVE, ____onMouseLeave);
		if (C.hovered)
		{
			this.hide();
		}
	}
	
	
	ns_despair function ____onTargetKilled( e:ManipulateEvent ) : void
	{
		var C:IComponent = e.target as IComponent;
		
		if (C.hovered)
		{
			this.hide();
		}
		
		delete m_toolTipList[ C ];
	}
	
	
	
	ns_despair var m_toolTipList:Dictionary = new Dictionary()
	
	ns_despair var m_pa:Persona
	
}

}

import org.despair2D.ui.ToolTipState

final class DefaultToolTipState extends ToolTipState
{
	
	override public function enter() : void
	{
		//var RTP:RawTextPuppet
		//
		//RTP = this.group.createRawTextPuppet()
		//RTP.defaultTextFormat = defaultFormat
		//RTP.htmlText = String(this.userData)
	}
	
	
	
	//internal static var defaultFormat:text = new TextFormat()
	
	internal static var m_bgColor:uint = 0xc3F3F3;
	
	internal static var m_bgAlpha:Number = 0.9

	internal static var m_lineColor:uint = 0;
	
	internal static var m_lineAlpha:Number = 1
	
	internal static var m_thickness:Number = 1;
		
	internal static var m_ellipse:Number = 2;
}